package com.sample.mockito.test;

import org.junit.Test;
import org.mockito.InOrder;

import java.util.LinkedList;
import java.util.List;

import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.*;

/*
https://www.cnblogs.com/s1328/p/4626757.html
 */
public class SampleTest02 {

    @Test(expected = RuntimeException.class)
    public void sampleTest5() {
        LinkedList mockedList = mock(LinkedList.class);

        //Stubbing void methods with exceptions
        doThrow(new RuntimeException()).when(mockedList).clear();

        //following throws RuntimeException:
        mockedList.clear();

    }

    @Test
    public void sampleTest6() {
        // A. Single mock whose methods must be invoked in a particular order
        List singleMock = mock(List.class);

        //using a single mock
        singleMock.add("was added first");
        singleMock.add("was added second");

        //create an inOrder verifier for a single mock
        InOrder inOrder = inOrder(singleMock);

        //following will make sure that add is first called with "was added first, then with "was added second"
        inOrder.verify(singleMock).add("was added first");
        inOrder.verify(singleMock).add("was added second");

        // B. Multiple mocks that must be used in a particular order
        List firstMock = mock(List.class);
        List secondMock = mock(List.class);

        //using mocks
        firstMock.add("was called first");
        secondMock.add("was called second");

        //create inOrder object passing any mocks that need to be verified in order
        InOrder inOrder2 = inOrder(firstMock, secondMock);

        //following will make sure that firstMock was called before secondMock
        inOrder2.verify(firstMock).add("was called first");
        inOrder2.verify(secondMock).add("was called second");

        // Oh, and A + B can be mixed together at will
    }



    @Test
    public void sampleTest07() {
        LinkedList mockedList = mock(LinkedList.class);
        //using mocks
        mockedList.add("one");

        verify(mockedList).add("one");
        verify(mockedList, never()).add("two");
    }

    @Test
    public void sampleTest08() {
        LinkedList mockedList = mock(LinkedList.class);
        //using mocks
        mockedList.add("one");
        mockedList.add("two");

        verify(mockedList).add("one");
        verify(mockedList).add("two");

        //following verification will fail
        verifyNoMoreInteractions(mockedList);
    }

}
